package com.chris.getfile.comm;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.CollectionType;
import org.slf4j.LoggerFactory;

import java.util.*;

/**
 * jackjson 序列化 和 反序列化 类
 * 重要
 * 苏斌 2021-9-3
 * 2022-7-12 增加 map , list 支持
 */
//@Component(value = "main_JacksonUtil")
//@Primary
public class JacksonUtil {

    //(屏蔽)ObjectMapper objectMapper =GetobjectMapper();
    //统一从config取
    //@Autowired
    ObjectMapper objectMapper;

    //构造函数从config注入
    //@Autowired
    public JacksonUtil(ObjectMapper om)
    {
        objectMapper=om;
    }
    private  org.slf4j.Logger log= LoggerFactory.getLogger(JacksonUtil.class);


    /**
     * object -> T ob 转 对象
     * BalanceSheetEO eo = jacksonUtil.readValue(obc,BalanceSheetEO.class);
     * @param ob  注意这里特指 map 、 obeject ，非 String
     * @param valueType
     * @param <T>
     * @return <T> ---> ob
     */
    public  <T> T readValue(Object ob, Class<T> valueType) {
        if (objectMapper == null) {
            objectMapper = new ObjectMapper();
        }
        //忽略未知的属性，导致所有的故障
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);
        try {
            return objectMapper.convertValue(ob, valueType);
        } catch (Exception e) {
            log.error(e.getMessage());
        }

        return null;
    }

    /**
     * object -> T ob 转 对象
     * BalanceSheetEO eo = jacksonUtil.readValue(obc,BalanceSheetEO.class);
     * @param jsonStr 注意，这个特指string ,非ob
     * @param valueType
     * @return
     */
    public  <T> T readValue(String jsonStr, Class<T> valueType) {
        if (objectMapper == null) {
            objectMapper = new ObjectMapper();
        }
        //忽略未知的属性，导致所有的故障
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);
        try {
            return objectMapper.readValue(jsonStr, valueType);
        } catch (Exception e) {
            log.error(e.getMessage());
        }

        return null;
    }

    /**
     *  object -> List<T> ob 转 对象 方法一
     *  mapper.readValue(listStr,new TypeReference<List<MltWaitLendReco>>() { });
     * @param ob json 、 map 、 obeject
     * @param valueTypeRef
     * @param <T>
     * @return List<T> --> list<ob>
     */

    public  <T> T readValue(Object ob, TypeReference<T> valueTypeRef){
        if (objectMapper == null) {
            objectMapper = new ObjectMapper();
        }
        //忽略未知的属性，导致所有的故障
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);
        try {

            return objectMapper.convertValue(ob, valueTypeRef);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return null;
    }

    /**
     *  object -> List<T> ob 转 对象 方法二
     *  mapper.readValue(listStr,new TypeReference<List<MltWaitLendReco>>() { });
     * @param ob json 、 map 、 obeject
     * @param valueType
     * @param <T>
     * @return List<T> --> list<ob>
     */
    public  <T> List<T> readValueToList(Object ob, Class<T> valueType){
        if (objectMapper == null) {
            objectMapper = new ObjectMapper();
        }
        CollectionType listType = objectMapper.getTypeFactory().constructCollectionType(ArrayList.class, valueType);
        //忽略未知的属性，导致所有的故障
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);
        try {

            return objectMapper.convertValue(ob, listType);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return null;
    }

    /**
     * json --> List
     * @param json 注意：--》json文本,不能是object --》 如果是对象，需要先转 json
     * @return list 子对象，如果map,返回的是 linkMap,非hashMap
     */
    public List readValueToList(String json) {
        if (objectMapper == null) {
            objectMapper = new ObjectMapper();
        }
        //忽略未知的属性，导致所有的故障
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        try {
            List list;
            list = objectMapper.readValue(json, ArrayList.class);
            return list;
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return null;
    }

    /**
     * json --> map
     *
     * @param json 注意：--》json文本,不能是object --》 如果是对象，需要先转 json
     * @return 子项目返回的是  hashMap， 非linkMap , 如果是对象里面还有子对象，子对象是linkMap
     */
    public Map readValueToMap(String json) {
        if (objectMapper == null) {
            objectMapper = new ObjectMapper();
        }
        //忽略未知的属性，导致所有的故障
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        try {
            Map map;// = new HashMap<String,Object>();
            map= objectMapper.readValue(json, HashMap.class);
            return map;
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return null;
    }




    /**
     * object to Integer
     * @param ob
     * @return
     */
    public  Integer readValueToInteger(Object ob) {
        if (objectMapper == null) {
            objectMapper = new ObjectMapper();
        }
        //忽略未知的属性，导致所有的故障
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);
        try {
            return objectMapper.convertValue(ob, Integer.class);
        } catch (Exception e) {
            log.error(e.getMessage());
        }

        return null;
    }

    /**
     * object to double
     * @param ob
     * @return
     */
    public  double readValueTodouble(Object ob) {
        if (objectMapper == null) {
            objectMapper = new ObjectMapper();
        }
        //忽略未知的属性，导致所有的故障
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);
        try {
            return objectMapper.convertValue(ob, double.class);
        } catch (Exception e) {
            log.error(e.getMessage());
        }

        return -9999;
    }

    /**
     * object to float
     * @param ob
     * @return
     */
    public  float readValueTofloat(Object ob) {
        if (objectMapper == null) {
            objectMapper = new ObjectMapper();
        }
        //忽略未知的属性，导致所有的故障
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);
        try {
            return objectMapper.convertValue(ob, float.class);
        } catch (Exception e) {
            log.error(e.getMessage());
        }

        return -99999;
    }

    /**
     * object to Date
     * @param ob
     * @return
     */
    public  Date readValueToDate(Object ob) {
        if (objectMapper == null) {
            objectMapper = new ObjectMapper();
        }
        //忽略未知的属性，导致所有的故障
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);
        try {
            return objectMapper.convertValue(ob, Date.class);
        } catch (Exception e) {
            log.error(e.getMessage());
        }

        return null;
    }





    /**
     * obj to String
     * 苏斌 2021-9-3 重要，主要可以做contorller 返回前端，时间格式转换
     * @param obj
     * @return
     */
    public String writeValueAsString(Object obj) {
        if (objectMapper == null) {
            objectMapper = new ObjectMapper();
        }
        String result = null;
        try {
            result = objectMapper.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            log.error("objectMapper writeValueAsString exception, e:", e);
        }
        return result;
    }

    /**
     * obj to String
     * 苏斌 2021-9-3 重要，主要可以做contorller 返回前端，时间格式转换
     *
     * @param obj
     * @return
     */
    public String writeValueAsString(Object obj,boolean isNON_EMPTY) {
        if (objectMapper == null) {
            objectMapper = new ObjectMapper();
        }
        String result = null;
        try {
            if (isNON_EMPTY)
                objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
            else
                objectMapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);
            result = objectMapper.writeValueAsString(obj);

        } catch (JsonProcessingException e) {
            log.error("objectMapper writeValueAsString exception, e:", e);
        }
        return result;
    }



}


